#ifndef _CANARY2_HTTP_RECV_REQUEST_AW_H_
#define _CANARY2_HTTP_RECV_REQUEST_AW_H_

#include "../net/coro_tcp.h"
#include "../http/chunked_buffer.h"
#include <coroutine>
#include <memory>
#include <system_error>
#include "../coro/epoll_suspended_manager.h"
#include "../net/tcp_server.h"
#include "request.h"

namespace wilson::http
{
    class recv_request_aw : private net::recv_aw
    {
    public:
        using storage_type = chunked_buffer;

    public:
        recv_request_aw(coro::epoll_suspend_manager& esm, 
                        net::tcp_server_context& ctx, 
                        storage_type stor, 
                        ::std::error_code* ec = nullptr)
            : net::recv_aw{ 
                  esm, 
                  ctx.get_io_ref(), 
                  stor.next_write_block(), 0, ec
              }, 
              m_storage{ ::std::make_shared<storage_type>(::std::move(stor)) },
              m_tcp_ctx{ ctx }, 
              m_suspend_manager{ esm }, 
              m_ec_ptr{ ec }
        {
        }

        bool await_ready();
        void await_suspend(::std::coroutine_handle<> h);
        server::request await_resume();

    private:
        ::std::shared_ptr<storage_type> m_storage;
        net::tcp_server_context& m_tcp_ctx;
        coro::epoll_suspend_manager& m_suspend_manager;
        ::std::error_code* m_ec_ptr;
    };
}

#endif
